package cn.hinglo.gameclaw.alipay.utils;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @createTime: 2018/5/16
 * @author: HingLo
 * @description: 支付宝支付工具
 */
public class AlipayUtils {


    private static String APP_ID = "2016091400507940";
    private static String APP_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCPqBD7MyNkwM3LQykBquKBGIwO6GoY3YcEFcUpA0OMpIkl9r8BK4Wch7DEoXf5Ad6kd0LKYUdp6asjVi9ltU7bK8GvlL8UEvc6epuXxpkEpnnVO4YLOvdPav7yU3CvKtAd6e+RaG3FgJTgyPDasw45v+j17Y3Nn6Bp66NIK/Jh2d7SiOKG+tMCFNohKyhlIzdNDZ6E/CjtN+XkdlzpNUA369Wy/DEiARacMcRr3Y8uit8nHKaMsq9T0ie62ZUEddRhDMhQT1Tsf6k/Yr8IgZybxaeBbNoiHIXjBgqngBxtJSsX4Dsgie5FYRo8vB/ncaHkM82z8rs2j2bp3HZwsjZPAgMBAAECggEAR2OFzM+E7zTBEBm37xQ0q2K/d0kYMHMl/Wm0KMVudB3v6eXM1ZADPd5Qgeab8Cp1sdVyY5AkJQXx/gV+WFTZcyEzh0WsDxW9tlnnqG0Q7batJMIHwtzyuYy6VUIR5XqpIbR3URNMObHESlCRIJ2KnDg6VLxQk7aXyQtIgT0VnnobzmJgP3vIHyGuRV/TK+21mVeIRRIUgZpmtSN+3B+97kNwgiZI+iMk8+wJ3bTz3G/vV+CwbIp7vBLp8qEmDJ1CTqXwUCRi22LVXKKCUeZMfdH8kg/cma+eZFPzMJ/1b7ryYwu/aXXvaA5gES3gjxLJ5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yLH3dCJ7ctolwYVU7Alq6ZYjY/cfjHqTKJXgOu+bFrujtbLSik5dLSfeLvC75RT1cuV2lruXLm5tx5yQO8hzuQV1PvTxXVsIjJFJHGSFZZ23ihh4JM6ov5q8api+IDa2JFV0uGvGXSTlCNN/La8NlLgQIKVKYDiIdmg3c52yAhrFKS/hm0ZOShQK04hTfFGknBakfnvUtc69oPmm69LPQuJjcpo18Yv1RJpEtwvZ8jhFYNVM/aH1/6PTzDEHcY/I6CQIDAQAB";

    /***
     * 给客服端响应已订单信息
     * @param model 订单的唯一Id
     * @return
     */
    public static String order(AlipayTradeAppPayModel model, String notifyUrl) {
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", APP_ID, APP_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, "RSA2");
        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
        //SDK已经封装掉了公共参数，这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
        request.setBizModel(model);
        request.setNotifyUrl(notifyUrl);
        try {
            //这里和普通的接口调用不同，使用的是sdkExecute
            AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
            //就是orderString 可以直接给客户端请求，无需再做处理。
            return response.getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 验证支付是否成功
     *
     * @param request 请求对象
     * @return 返回是否成功
     */
    public static Map<String, String> validate(HttpServletRequest request) {
        Map<String, String> params = new HashMap<>();
        Map<String, String[]> requestParams = request.getParameterMap();
        for (String name : requestParams.keySet()) {
            String[] values = requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                        : valueStr + values[i] + ",";
            }
            params.put(name, valueStr);
        }
        try {
            boolean bool = AlipaySignature.rsaCheckV1(params, ALIPAY_PUBLIC_KEY, CHARSET, "RSA2");
            return bool ? params : null;
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return null;
    }

}
